package 数组.easy;

import java.util.Arrays;

/**
 * @author Lw中
 * @date 2020/9/12 10:21
 */
public class YiLuQi {

    /**
     * 两数之和 II - 输入有序数组
     * @param numbers
     * @param target
     * @return
     */
    public static int[] twoSum(int[] numbers, int target) {
        /* 方法一：循环遍历 */
//        for (int i = 0; i < numbers.length; i++) {
//            for (int j = i+1; j < numbers.length; j++) {
//                if (numbers[i] <= numbers[j] && numbers[i] + numbers[j] == target) {
//                    return new int[]{i+1, j+1};
//                }
//            }
//        }
//        return new int[]{-1, -1};
        /* 方法二：双指针 */
        int low = 0, high = numbers.length - 1;
        while (low < high) {
            int sum = numbers[low] + numbers[high];
            if (sum== target) {
                return new int[]{low + 1, high + 1};
            } else if (sum < target) {
                low++;
            } else {
                high--;
            }
        }
        return new int[]{-1,-1};
    }

    public static void main(String[] args) {
        int[] arr = {2,7,11,15};
        int[] ints = twoSum(arr, 9);
        System.out.println(Arrays.toString(ints));
    }

}
